package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ItemDao;
import cn.topevery.garden.entity.fetch.ItemFetch;
import cn.topevery.garden.entity.po.Item;
import cn.topevery.garden.entity.query.ItemQuery;
import cn.topevery.garden.entity.vo.ItemVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 绿地
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class ItemDaoImpl extends BaseDaoImpl<Item> implements ItemDao {
    @Override
    public <P extends DbCsp> List<Item> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ItemVO> listPage(ItemQuery query) {
        String table = "t_item ti";

        String columns = "ti.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ti.c_id desc");
        sqlQueryMate
                .where(true, "ti.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ItemVO.class);
    }

    @Override
    public Item getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<Item> getListByMapId(ItemFetch para) {
        StringBuffer sb = new StringBuffer();
        sb.append("select * from t_item where c_db_status = 0  and c_district = "+para.getMapId());
        sb.append(" and  ( c_is_new = 1 OR ( c_is_new IS NULL )) ");

        AtomicReference<List<Item>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(Item.class));
        });

        return vo.get();
    }
}
